package util;

import java.io.File;
import java.util.Observable;
import java.util.Observer;

import android.os.Environment;

public final class VerifyInstallation {
    private VerifyInstallation() {
    }

    private static boolean basedir = false;
    private static boolean mapdir = false;
    private static boolean graphicsdir = false;
    private static boolean configuration = false;
    private static boolean soundsdir = false;
    private static boolean netdir = false;

    private static String cwBaseDir;

    private final static String[] mapDirs;
    private static String[] graphicDirs;

    private static boolean create = true;

    public static final String SEPERATOR;

    private static MapListObserver observable;

    static {
        SEPERATOR = System.getProperty("file.separator");

        observable = new MapListObserver();
        cwBaseDir = Environment.getExternalStorageDirectory() + "/canvaswarfare";

        if (validateDir(cwBaseDir)) {
            basedir = true;
        }

        if (validateDir(cwBaseDir + SEPERATOR + "graphics")) {
            graphicsdir = true;
            graphicDirs = getDirs(cwBaseDir + "" + SEPERATOR + "graphics");
        }

        mapDirs = initMapDirs();

        if (validateDir(cwBaseDir + SEPERATOR + "net")) {
            netdir = true;
        }

        if (validateDir(cwBaseDir + SEPERATOR + "sounds")) {
            soundsdir = true;
        }

        final File conf = new File(cwBaseDir + SEPERATOR + "config.xml");
        if (conf.exists() && conf.isFile()) {
            configuration = true;
        }
    }

    private static String[] initMapDirs() {
        if (validateDir(cwBaseDir + SEPERATOR + "maps")) {
            mapdir = true;
            return getDirs(cwBaseDir + SEPERATOR + "maps");
        } else {
            return null;
        }
    }

    public static String[] getResolutions(final String themePath) {
        int i;
        for (i = 0; i < graphicDirs.length; i++) {
            if (graphicDirs[i].equals(themePath)) {
                break;
            }
        }

        if (i == graphicDirs.length) {
            return null;
        }

        final String themeDir = new String(cwBaseDir + SEPERATOR + "graphics" + SEPERATOR + themePath);

        return getDirs(themeDir);
    }

    private static boolean validateDir(final String s) {
        final File dir = new File(s);
        if (dir.exists() && dir.isDirectory()) {
            return true;
        } else if (create) {
            return dir.mkdir();
        } else {
            return false;
        }
    }

    private static String[] getDirs(final String s) {
        final File f = new File(s);
        final String[] tmp = f.list();
        int x = 0;

        for (int i = 0; i < tmp.length; i++) {
            final File tf = new File(s + SEPERATOR + tmp[i]);
            if (!tf.isDirectory()) {
                tmp[i] = null;
            } else {
                x++;
            }
        }

        final String[] dirs = new String[x];
        x = 0;
        for (int i = 0; i < tmp.length; i++) {
            if (tmp[i] != null) {
                dirs[x] = tmp[i];
                x++;
            }
        }

        return dirs;
    }

    public static boolean basedirExists() {
        return basedir;
    }

    public static boolean mapdirExists() {
        return mapdir;
    }

    public static boolean graphicsdirExists() {
        return graphicsdir;
    }

    public static boolean netdirExists() {
        return netdir;
    }

    public static boolean soundssdirExists() {
        return soundsdir;
    }

    public static boolean configurationsFileExists() {
        return configuration;
    }

    public static String[] getMapDirs() {
        return mapDirs;
    }

    public static String[] getGraphicDirs() {
        return graphicDirs;
    }

    public static String getCvBaseDir() {
        return cwBaseDir;
    }

    public static void addMapList(final Observer o) {
        observable.addObserver(o);
    }

    private static class MapListObserver extends Observable {

        public MapListObserver() {
            super();
        }

        public void changed() {
            super.setChanged();
        }
    }

    public static void mapDeletedOrAdded() {
        initMapDirs();
        observable.changed();
        observable.notifyObservers(null);
    }
}
